package BuildData;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import java.util.StringTokenizer;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;

/**
 *
 * @author Administrator
 */
public final class GreekTreeData {

    private ArrayList itemAr = new ArrayList();
    private ArrayList tokenAr = new ArrayList();
private ArrayList greekAr=new  ArrayList();
    
    public GreekTreeData() throws FileNotFoundException, XMLStreamException, IOException{

        System.out.println("package BuildData; class GreekTreeData");

        setNswAr();
        setGreek();
        setItemAr();
        
        writeToFile();       
    }

    public void setNswAr() throws FileNotFoundException, XMLStreamException {
        FileInputStream fis = new FileInputStream("trainset\\hand\\alpha_hand.xml");
        XMLStreamReader xmlStreamReader = XMLInputFactory.newInstance().createXMLStreamReader(fis);

        while (xmlStreamReader.hasNext()) {
            int eventCode = xmlStreamReader.next();
            switch (eventCode) {
                case XMLStreamConstants.START_ELEMENT:
                    String localName = xmlStreamReader.getLocalName();
                    if (localName.equals("W")) {
                        String attributeValue = xmlStreamReader.getAttributeValue(0);
                        
                        String nsw = xmlStreamReader.getElementText();
                        

                        Token tkn = new Token();
                        tkn.setValue(nsw);
                        tkn.setNsw(attributeValue);
                        tokenAr.add(tkn);


                    }else{
                        if(localName.equals("URL")){
                            String url=xmlStreamReader.getElementText();
                        }
                    }
                    break;
                case XMLStreamConstants.CHARACTERS:
                    String character = xmlStreamReader.getText();

                    StringTokenizer st = new StringTokenizer(character);
                    while (st.hasMoreTokens()) {
                        String tokenStr=st.nextToken();
                        Token token=new Token();
                        token.setValue(tokenStr);
                        token.setNsw("NOT");
                        tokenAr.add(token);

                    }

            }
        }


    }

    public void setItemAr()  {
        ArrayList nswTrainSet=new ArrayList();
        for(int i=0;i<tokenAr.size();i++){
            Token tkn= new Token();
            tkn=(Token)tokenAr.get(i);           
            if(isGreekNumber(tkn.getValue())){
                ALPHA nsw=new ALPHA();
                String tknValue=tkn.getValue();
                String tknNsw=tkn.getNsw();
                nsw.setclassNsw(tknNsw);

                nsw.settknAf1(getToken(i+1));
                nsw.settknAf2(getToken(i+2));
                nsw.settknBf1(getToken(i-1));
                nsw.settknBf2(getToken(i-2));
                nsw.setNswStr(tknValue);
                nswTrainSet.add(nsw); 
            }
            
        }
        itemAr=nswTrainSet;
      

    }

    public void writeToFile() throws IOException {
    
      BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("data\\tree\\greektreedata.db"), "UTF8"));
      bw.write("tree-database\n");
      bw.write("object name tknBf2 symbolic tknBf1 symbolic nswStr symbolic tknAf1 symbolic tknAf2 symbolic classNsw symbolic\n");
           
            ArrayList ar = new ArrayList();
            ar = getItemAr();
            String item="";
            for (int i = 0; i < ar.size(); i++) {
                item="";
                ALPHA nsw = (ALPHA) ar.get(i);
                item+=i;                   
               
               
                String tknBf2=nsw.gettknBf2();
                item+=" "+tknBf2;
                String tknBf1=nsw.gettknBf1();
                item+=" "+tknBf1;
                String nswStr=nsw.getNswStr();
                item+=" "+nswStr;
                String tknAf1=nsw.gettknAf1();
                item+=" "+tknAf1;
                String tknAf2=nsw.gettknAf2();
                item+=" "+tknAf2;
                
                String classNsw=nsw.getclassNsw();
                item+=" "+classNsw;
                
                 
               item+="\n";
                bw.write(item);
                    
            }
            bw.close();
           
    }

    public ArrayList getItemAr() {
        return itemAr;
    }

   public int getNumCount(String nsw){
       int count=0;
       for(int i=0;i<nsw.length();i++){
           if(Character.isDigit(nsw.charAt(i))){
               count++;
           }
       }
       return count;
   }

   public String getToken(int index){
       if(index<0||index>=tokenAr.size()){
           return "";
       }
       else{
           Token tkn=(Token)tokenAr.get(index);
           return tkn.getValue();
       }

   }

   
 public boolean isNum(String nsw) {
        for (int i = 0; i < nsw.length(); i++) {
            if (Character.isDigit(nsw.charAt(i))) {
                return true;
            }
        }
        return false;
    }

     public boolean isGreekNumber(String nsw) {
        if (greekAr.contains(nsw)) {
            return true;
        }
        return false;
    }
   public void setGreek() throws UnsupportedEncodingException, FileNotFoundException, IOException {
//        greekAr.add("I");
//        greekAr.add("II");
//        greekAr.add("III");
//        greekAr.add("IV");
//        greekAr.add("V");
//        greekAr.add("VI");
//        greekAr.add("VII");
//        greekAr.add("VIII");
//        greekAr.add("IX");
//        greekAr.add("X");
//        greekAr.add("XI");
        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("resources\\greekNumber.db"), "UTF8"));
        String greekNumber;
        int i = 0;
        while ((greekNumber = br.readLine()) != null) {
            greekAr.add(greekNumber);
//            i++;
        }
    }
}
